##################################
##################################
###Figure 4.1: Feature Barplots###
##################################
##################################
library(gganimate)
library(ggplot2)
library(maps)
library(ggthemes)
library(doBy)
#Set WD
setwd("~/OneDrive - Indiana University/FromGoogle/SouthSudanProject/Code_12_20_24/")
#Import data
ALLNGO <- read.csv("ingodatmap_9_20_24.csv")
summary(ALLNGO)

#############################
###Clean and Organize Data###
#############################
#Set up categories
ALLNGO$Main <- ifelse(ALLNGO$Climate.Adaptation==1,"Climate adaptation",
                              ifelse(ALLNGO$Environmental.Preservation==1, "Environmental preservation",
                                     ifelse(ALLNGO$Environmental.Management==1, "Environmental management",
                                     ifelse(ALLNGO$Food.Security==1, "Food security",
                                            ifelse(ALLNGO$Conflict.prevention.early.warning==1, "Preventing conflict",
                                                   ifelse(ALLNGO$Peacebuilding == 1, "Peacebuilding",
                                                          ifelse(ALLNGO$Early.Warning == 1, "Warning","Other")))))))
table(ALLNGO$Main)

#Create an aggregated indicator for CAFSI
ALLNGO$tot <- ifelse((ALLNGO$Climate.Adaptation==1|ALLNGO$Environmental.Preservation==1|ALLNGO$Environmental.Management==1|ALLNGO$Food.Security==1),1,0) 
table(ALLNGO$tot)
#Create an aggregated indicator for CPP
ALLNGO$cp <- ifelse((ALLNGO$Conflict.prevention.early.warning==1|ALLNGO$Peacebuilding == 1|ALLNGO$Early.Warning == 1),1,0)
table(ALLNGO$cp)
#Create an exclusive for multiple intervention variables
ALLNGO$cafsi <- ifelse(ALLNGO$tot==1,"CAFSI","CPP")
table(ALLNGO$cafsi)

#######################
###Plot Figure 4.1a ###
#######################
#Define a program focus variable
ALLNGO$`Program Focus` <- as.factor(ALLNGO$Main)
#Remove NAs
ALLNGO.s <- ALLNGO[!is.na(ALLNGO$`Program Focus`),]
#Plot figure
jpeg("Figure41a.jpeg", width = 6, height = 6, units = 'in', res = 500)
ggplot(data=ALLNGO.s, aes(cafsi))+
  geom_bar(aes(fill=`Program Focus`), position="fill")+
  ylab("Share (percent)")+xlab("")+
  scale_y_continuous(breaks=c(0,0.2,0.4,0.6,0.8,1), label = c("0","20","40","60","80","100"))+
  scale_fill_brewer(palette = "GnBu")
dev.off() 

#######################
###Plot Figure 4.1a ###
#######################
##Organize climate adaptation by prepradness type
#subset only cases with en intervention
ALLNGO.t <- subset(ALLNGO.s, tot==1)
##General preparedness data
#remove NAs
ALLNGO.t$General.Preparedness[is.na(ALLNGO.t$General.Preparedness)] <- 0
summary(ALLNGO.t$General.Preparedness)
#Change to a binary response variable
ALLNGO.t$f.gs <- ifelse(ALLNGO.t$General.Preparedness==1, "Yes", "No")

##Community building data
#remove NAs
ALLNGO.t$Community.Building[is.na(ALLNGO.t$Community.Building)] <- 0
summary(ALLNGO.t$Community.Building)
#Change to a binary response variable
ALLNGO.t$f.cb <- ifelse(ALLNGO.t$Community.Building==1, "Yes", "No")

##Create a general indicator for plotting
ALLNGO.t$`Measure` <- ifelse((ALLNGO.t$f.gs=="Yes"&ALLNGO.t$f.cb=="Yes"), "Both",
                             ifelse((ALLNGO.t$f.gs=="Yes"&ALLNGO.t$f.cb=="No"), "General prepardness only",
                                    ifelse((ALLNGO.t$f.gs=="No"&ALLNGO.t$f.cb=="Yes"), "Community building only", "Neither")))
table(ALLNGO.t$`Measure`)

##Plot the figure
jpeg("Figure41b.jpeg", width = 7, height = 6, units = 'in', res = 500)
ggplot(data=ALLNGO.t, aes(`Program Focus`))+
  geom_bar(aes(fill=`Measure`), position="fill")+
  ylab("Share (percent)")+xlab("Program focus")+
  scale_y_continuous(breaks=c(0,0.2,0.4,0.6,0.8,1), label = c("0","20","40","60","80","100"))+
  scale_fill_brewer(palette = "BuPu")
dev.off()                             
                            

